<template>
  <div id="main">
    <h2>成果转化</h2>

    <el-button
      type="primary"
      style="margin-top: 0px; margin: 20px"
      @click="handleOpen('添加学术', 1)"
    >添加学术</el-button>
    <!-- </el-col> -->
    <el-input
      v-model="searchTableInfo"
      placeholder="请输入搜索内容"
      style="width: 240px; float: right; margin-top: 0px; margin-right: 30px"
    />
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="academicName" label="学术名称" />
        <el-table-column align="center" prop="staffName" label="学术责任人" />
        <el-table-column
          align="center"
          prop="academicSchedule"
          label="学术执行进度"
        />
        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改学术', 2, Scope.row.academicId)"
            >编辑
            </el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(Scope.row.academicId, Scope.row.staffName)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="academic"
        :model="academic"
        label-position="top"
        :rules="rules"
      >
        <el-form-item
          prop="academicName"
          label="学术名称"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicName"
            autocomplete="off"
            maxlength="15"
            show-word-limit
          />
        </el-form-item>
        <el-form-item
          prop="academicMeaning"
          label="学术意义"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicMeaning"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicTarget"
          label="学术目标"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicTarget"
            autocomplete="off"
            maxlength="100"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 10 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicPersonId"
          label="学术责任人"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="academic.academicPersonId"
            filterable
            placeholder="请选择"
            style="width: 40%"
          >
            <el-option
              v-for="item in teacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="academicSchedule"
          label="学术执行进度"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicSchedule"
            autocomplete="off"
            maxlength="100"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 10 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicPhasesum"
          label="学术阶段总结"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicPhasesum"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicAchievement"
          label="学术成果"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicAchievement"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicCompleteeva"
          label="学术完成评价"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicCompleteeva"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="academicImprovementeva"
          label="学术改进计划"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="academic.academicImprovementeva"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('academic')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('academic')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  updateacademic,
  deleteacademic,
  getAllachievement,
  getacademicById,
  addachievement
} from '@/api/academic'
import { getTeacherString } from '@/api/attendanceStu'
export default {
  data() {
    return {
      newAca: {
        academicName: '',
        academicId: '',
        staffName: '',
        academicSchedule: ''
      },
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      searchTableInfo: '',
      academic: [],
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      teacher: {},
      search: '',
      formLabelWidth: '200px',
      rules: {
        academicAchievement: [
          {
            required: true,
            message: '学术成果不能为空',
            trigger: 'blur'
          }
        ],
        academicCompleteeva: [
          {
            required: true,
            message: '学术完成评价不能为空',
            trigger: 'blur'
          }
        ],
        academicImprovementeva: [
          {
            required: true,
            message: '学术改进评价不能为空',
            trigger: 'blur'
          }
        ],
        academicId: [
          {
            required: true,
            message: '学术id不能为空',
            trigger: 'blur'
          }
        ],
        academicMeaning: [
          {
            required: true,
            message: '学术意义不能为空',
            trigger: 'blur'
          }
        ],
        academicName: [
          {
            required: true,
            message: '学术名称不能为空',
            trigger: 'blur'
          }
        ],
        academicPersonId: [
          {
            required: true,
            message: '学术责任人不能为空',
            trigger: 'blur'
          }
        ],
        academicPhasesum: [
          {
            required: true,
            message: '学术阶段总结不能为空',
            trigger: 'blur'
          }
        ],
        academicSchedule: [
          {
            required: true,
            message: '学术执行进度不能为空',
            trigger: 'blur'
          }
        ],
        academicTarget: [
          {
            required: true,
            message: '学术目标不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getAllTeachers()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getAllTeachers() {
      getTeacherString().then(res => {
        this.teacher = res.obj
      })
    },

    handleClick(row) {
      console.log(row)
    },

    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllachievement().then(data => {
        this.newAca
        for (var i = 0; i < data.obj.length; i++) {
          this.newAca.academicName = data.obj[i].academicName
          this.newAca.academicId = data.obj[i].academicId
          this.newAca.staffName = data.obj[i].staff.staffName
          this.newAca.academicSchedule = data.obj[i].academicSchedule

          this.tableList.push(this.newAca)
          this.newAca = {}
        }
        this.listLoading = false
      })
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该学生？姓名为' + username).then(() =>
        deleteacademic(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },

    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addachievement(this.academic).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.academic = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.academic = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        getacademicById(id).then(resp => {
          this.academic = resp.obj
        })
      }
    },

    handleClose() {
      this.dialogFormVisible = false
      this.academic = {}
    },

    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateacademic(this.academic).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}

#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
